package com.ruijie.autotest.log.impl.validate;

import com.ruijie.autotest.log.common.sql.SqlField;
import com.ruijie.autotest.log.core.validate.IValidator;

import javax.sql.DataSource;
import java.sql.SQLException;

public class SqlExistingValidator implements IValidator {
	private final SqlField sqlField;

	public SqlExistingValidator(SqlField sqlField) {
		this.sqlField = sqlField;
	}

	@Override
	public boolean validate(String actualValue) {
		try {
			return sqlField.existsValue(actualValue);
		} catch (SQLException e) {
			throw new RuntimeException("validation failed.", e);
		}
	}

	public static SqlExistingValidator sqlExistingVerificationOf(String pattern, DataSource dataSource) {
		return new SqlExistingValidator(SqlField.createSqlField(dataSource, pattern));
	}
}
